<?php

namespace app\admin\controller;
use think\Controller;
use think\File;
use think\Request;
use think\Seeeion;
use think\Db;
use org\Qiniu;
use org\Upayun;
use aliyun\OssDemo;
class Upload extends Base
{

    public function wangUpload(){
        foreach($_FILES as $key=>$vo){
            $filePath = $vo['tmp_name'];
            $type = explode(".",$vo['name']);
            $type = end($type);
            $key = md5(time().uuid()).'.'.$type;
            $up = new ossDemo();
            $name = $up->uploadFile ($filePath , $key);
            $data[] = "https://jintianjifen.oss-cn-shanghai.aliyuncs.com/{$name}";
        }
        return json(['errno'=>0,'data'=>$data]);
    }


    public function uploadLocality(){
        $filePath = $_FILES['file']['tmp_name'];
        $type = explode(".",$_FILES['file']['name']);
        $type = end($type);
        $key = md5(time().uuid()).'.'.$type;
        $up = new ossDemo();
        $data = $up->uploadFile($filePath,$key);
        echo "https://jintianjifen.oss-cn-shanghai.aliyuncs.com/{$data}";
    }


    public function deleteLocality(){
        $add = input('add');
        $add = str_replace ('https://jintianjifen.oss-cn-shanghai.aliyuncs.com/','',$add);
        $up = new ossDemo();
        $up->deleteObject($add);
        return json(['code'=>200,'msg'=>'删除成功！']);
    }


    public function updateFace(){
        $base64url = input('base64url');
        $arr = base64_img($base64url,true);
        if($arr['code'] == 200){
            $res = Db::name('admin')->where('id',input('id'))->update(["portrait"=>$arr['msg']]);
            if($res){
                session('portrait', $arr['msg']);
                return json(['code'=>200,'msg'=>"上传成功"]);
            }else{
                return json(['code'=>100,'msg'=>"上传失败"]);
            }
        }elseif($arr['code'] == 100){
            return json($arr);
        }
    }

}